﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace KATS_Client.UI
{
    /// <summary>
    /// This class generates the UI
    /// </summary>
    static class Generator
    {
        /// <summary>
        /// The window to make
        /// </summary>
        static MainWindow window;

        /// <summary>
        /// Make the main window's form elements
        /// </summary>
        /// <param name="window"></param>
        public static void Generate(MainWindow window2)
        {
            window = window2;

            GenerateWindows();
            UpdateLocationFields();
        }

        /// <summary>
        /// Generate the windows for the menu
        /// </summary>
        private static void GenerateWindows()
        {
            WindowManager.Initialise();
            AttachWindows();
        }

        /// <summary>
        /// Attach the windows to the main area in the winform
        /// </summary>
        public static void AttachWindows()
        {
            int numberWin = WindowManager.Windows.Count;

            // make the split containers for the windows
            SplitContainer[] sc = new SplitContainer[numberWin - 1];
            for (int i = 0; i < numberWin - 1; i++)
            {
                SplitContainer scs = new SplitContainer();
                scs.Dock = DockStyle.Fill;
                scs.SplitterWidth = 5;

                // calculate the position on the splitter distance
                double spread = 1.0/ (numberWin-i);
                double position = scs.Width * spread;
                scs.SplitterDistance = (int)(position);

                sc[i] = scs;

            }

            SplitContainer main = sc[0];

            // place the split containers into each other
            if (numberWin > 2)
            {
                for (int i = 0; i < numberWin - 2; i++)
                {
                    sc[i].Panel2.Controls.Add(sc[i + 1]);
                }
            }


            // now add the tab controls into each of the split containers
            // panels
            for (int i = 0; i < numberWin - 1; i++)
            {
                sc[i].Panel1.Controls.Add(WindowManager.Windows[i].panel);
            }
            sc[numberWin - 2].Panel2.Controls.Add(WindowManager.Windows[numberWin - 1].panel);



            window.MainArea.Controls.Clear();
            window.MainArea.Controls.Add(main);
        }

        /// <summary>
        /// Give all the location bars their initial values
        /// </summary>
        private static void UpdateLocationFields()
        {
            foreach (Window w in WindowManager.Windows)
            {
                w.Update_LocationBox();
            }
        }


        /// <summary>
        /// Make a button
        /// </summary>
        /// <param name="Text">Text to be displayed</param>
        /// <param name="ToolTip">The tool tip text</param>
        /// <param name="ButtonImage">The image to use for this image</param>
        /// <returns>The tool strip button</returns>
        public static ToolStripButton MakeButton(string Text, string ToolTip, System.Drawing.Image ButtonImage)
        {
            ToolStripButton button = new ToolStripButton();

            if (ButtonImage != null)
            {
                button.Image = ButtonImage;
                button.DisplayStyle = ToolStripItemDisplayStyle.Image;
            }
            if (Text != null)
                button.Text = Text;
            button.ToolTipText = ToolTip;

            return button;
        }
    }
}
